﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using IFClientDLL;
using IFClientDLL.Model;
using HCY.CTP.DBShangZheng;

using System.Linq;
using IFClientDemo;
using IFServerDLL.Model;
using FrmTryIF.Ctl.Demo;
using IFViewDetail.Ctl.Demo.PlayMarket;


namespace IFViewDetail.KuaiQi
{
    public partial class FrmDemoKuaiQi : DevExpress.XtraEditors.XtraForm
    {
        public FrmDemoKuaiQi()
        {
            InitializeComponent();
            ctlKuaiQi1.Enabled = false;
        }
        private CtlPlayMarket _player;
        public FrmDemoKuaiQi(CtlPlayMarket player)
        {
            _player = player;
            InitializeComponent();
            ctlKuaiQi1.Enabled = false;
        }
        TryIF_Demo _tryif;
        public ITryIF Init(HCY.CTP.DBShangZheng.DBTableMarketDataZi zi, string username)
        {
            ctlKuaiQi1.Enabled = true;

            _tryif = new TryIF_Demo(zi, this, username,this.backgroundWorker_DataPlayer,this.backgroundWorker_TradeOrder);
           
            _tryif.OnMessage += new Action<MessageBase>(_tryif_OnMessage);
            ctlKuaiQi1.InitTryIF(_tryif);
            if (_player != null)
            {
                _player.InitTryIF(_tryif);
            }
            barListItem_Msg.Caption = "";
            _tryif.DataServer.Start();
            return _tryif;
        }

        void _tryif_OnMessage(MessageBase obj)
        {
            if (barListItem_Msg.Strings.Count > 10)
            {
                barListItem_Msg.Strings.RemoveAt(0);
            }
            var str = DateTime.Now.ToString("HH:mm:ss fff") + "  " + obj.Msg;
            barListItem_Msg.Strings.Add(str);
            barListItem_Msg.Caption = str;
        }

        private void FrmDemoKuaiQi_Shown(object sender, EventArgs e)
        {

        }

        private void FrmDemoKuaiQi_KeyDown(object sender, KeyEventArgs e)
        {
            var data = ctlKuaiQi1.GetFocuseIF();
            if (data == null)
                return;
            if (UserInstrumentOption.ListUserInstrumentOption.Where(p => p.KeyName == e.KeyCode).Any())
            {
                _tryif.AddUserCommand(new UserCommand(data.Instrument, data.BuyPrice, data.SellPrice, e.KeyCode), Guid.Empty);
            }
            else
            {
                _tryif.AddMessage(new MessageBase("error command key " + e.KeyCode.ToString()));
            }
        }

        private void ctlKuaiQi1_Load(object sender, EventArgs e)
        {

        }

        private void barButtonItem_SelectDemoData_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
        }

        private void FrmDemoKuaiQi_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
    }
}